package net.htjs.juc.util;

import java.util.Map;

public class UserContextHolder {

    private final ThreadLocal<Map<String,String>> threadLocal;
    private static final UserContextHolder INSTANCE = new UserContextHolder();

    private UserContextHolder() {
        this.threadLocal = new ThreadLocal<>();
    }


    public static UserContextHolder getInstance() {
        return INSTANCE;
    }

    /**
     * 设置上下文
     * @param userContext
     */
    public void setContext(Map<String,String> userContext) {
        threadLocal.set(userContext);
    }

    /**
     * 获取上下文
     * @return
     */
    public Map<String,String> getContext() {
        return threadLocal.get();
    }

    /**
     * 移除上下文
     */
    public void remove() {
        threadLocal.remove();
    }
}
